//
//  EvaluateViewController.m
//  MM
//
//  Created by 郑渊文 on 6/1/15.
//  Copyright (c) 2015 王晨辉. All rights reserved.
//

#import "EvaluateViewController.h"
#import "CommonMacro.h"
#import "StarView.h"
#import "ReadEvaluationViewController.h"

@interface EvaluateViewController ()<StarViewDelegate,UITextViewDelegate>
{
    UITextView *myTextView;//评论输入框
    int starCount;//星星数量
    UIButton *submitBtn; //提交按钮
    UILabel *sourceLab; //评价
    UILabel *placeholder_lable;//评论holder
    
}
@end

@implementation EvaluateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"课程详情";
    
    starCount = 3;
    [self initSubViews];
    //添加单击手势
    
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)initSubViews
{
    //自定义左右按钮
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [button setBackgroundImage:[UIImage imageNamed:@"_0000_返回.png"]
     
                      forState:UIControlStateNormal];
    
    [button addTarget:self action:@selector(back)
     
     forControlEvents:UIControlEventTouchUpInside];
    
    button.frame = CGRectMake(0, 0, 20, 15);
    
    
    
    UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    
    
    
    self.navigationItem.leftBarButtonItem = menuButton;
    
    
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    bgView.backgroundColor = UIColorWithRGBA(240, 239, 245, 1);
    [self.view addSubview:bgView];
    
    
    sourceLab = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-100, 90,80, 30)];
    sourceLab.text = @"好";
    [bgView addSubview:sourceLab];
    
    submitBtn = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-100, 10,80, 30)];
    [submitBtn setTitle:@"发布" forState:UIControlStateNormal];
    [submitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [submitBtn addTarget:self action:@selector(submitEvaluation:) forControlEvents:UIControlEventTouchUpInside];
    submitBtn.backgroundColor = UIColorWithRGBA(219, 99, 101, 1);
    //    [RegistBtn setBackgroundColor:[UIColor clearColor]];
    [self.navigationController.navigationBar addSubview:submitBtn];
    
    
    StarView *startView = [[StarView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2-40,90, 80, 21)];
    [bgView addSubview:startView];
    startView.delegate =self;
    //    StarView.numberOfStar = 3;
    UIView *myEvaluationView = [[UIView alloc] initWithFrame:CGRectMake(10, 120 , SCREEN_WIDTH-20, 200)];
    [bgView addSubview:myEvaluationView];
    
    UITextView *textV = [[UITextView alloc] initWithFrame:CGRectMake(0, 0 , SCREEN_WIDTH-20, 200)];
    textV.delegate = self;
    textV.returnKeyType = UIReturnKeyDone;
    [textV setFont:[UIFont systemFontOfSize:16.0f]];
    myTextView = textV;
    placeholder_lable = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, SCREEN_WIDTH - 20, 20)];
    [placeholder_lable setTextColor:[UIColor lightGrayColor]];
    placeholder_lable.text = @"服务评价，限100字以内";
    [textV addSubview:placeholder_lable];
    [myEvaluationView addSubview:textV];
    
    UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(Taped:)];
    [myEvaluationView addGestureRecognizer:recognizer];
    
}


- (void)submitEvaluation:(UIButton *)sender
{
    NSDictionary *param4 = @{@"babyId":[BabyManager shareInstance].babyId,@"classId":@"1",@"grade":[NSString stringWithFormat:@"%d",starCount],@"suggtion":myTextView.text};
    
    [UserDAL getClasssuggestParams:param4 withBlock:^(NSDictionary *data, NSError *error) {
        if (data) {
            
             [self ShowProgressHUDwithMessage:data[@"msg"]];
            ReadEvaluationViewController *sugVC = [[ReadEvaluationViewController alloc]init];
            
            UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:sugVC];
            [self  presentViewController:nav animated:YES completion:nil];
            
        }


        
        else
        {
            
        }
    }];

    
    //此处传值
    myTextView.text = @"";
    sender.enabled = NO;
    placeholder_lable.hidden = NO;
    
   
}

#pragma mark - HUD
- (void)ShowProgressHUDwithMessage:(NSString *)msg
{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.labelText = msg;
    hud.dimBackground = NO;
    hud.removeFromSuperViewOnHide = YES;
    [hud hide:YES afterDelay:1];
}
#pragma mark - StarViewDelegate
- (void)starView:(StarView *)starView score:(int)score
{
    starCount = score;
    switch (starCount) {
        case 1:
            sourceLab.text = @"差";
            break;
        case 2:
            sourceLab.text = @"一般";
            break;
        default:
            sourceLab.text = @"好";
            break;
    }
}

- (void)Taped:(UITapGestureRecognizer *)recognizer
{
    if (myTextView.text.length == 0)
    {
        placeholder_lable.hidden = NO;
        submitBtn.enabled = NO;
    }
    [myTextView resignFirstResponder];
}

#pragma mark - UITextViewDelegate
- (void)textViewDidBeginEditing:(UITextView *)textView
{
    textView.text = @"";
}

- (void)textViewDidChange:(UITextView *)textView
{
    placeholder_lable.hidden = textView.text.length == 0 ?NO:YES;
    submitBtn.enabled = textView.text.length == 0 ?NO:YES;
}

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"])
    {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}

- (void)textViewDidEndEditing:(UITextView *)textView
{
    if (textView.text.length > 100)
    {
        textView.text = [textView.text substringToIndex:100];
    }
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    if ([myTextView isFirstResponder])
    {
        [myTextView resignFirstResponder];
    }
}
-(void)back
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
